package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.OfficialWebSiteJdbc;
import com.wuseguang.springcloud.service.OfficialWebSiteService;
import com.wuseguang.springcloud.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class OfficialWebSiteServiceImpl implements OfficialWebSiteService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OfficialWebSiteJdbc officialWebSiteJdbc;
    private String sql;


    /**
     * @描述 查询官网幻灯片
     * @创建人 zm
     * @创建时间 2020/7/10
     */
    @Override
    public Result queryCarouselAll() {
        String sql = "SELECT  id,url,link,sort FROM admin_carousel ORDER BY sort";
        List<Map<String, Object>> list = officialWebSiteJdbc.queryListMap(sql);
        return new Result(list);
    }


    /**
     * @描述 修改幻灯片
     * @创建人 zm
     * @创建时间 2020/7/10
     */
    @Override
    public Result updateCarouselById(Integer id, Integer sort) {
        String sql = "UPDATE admin_carousel SET sort=? WHERE id=?";
        int i = officialWebSiteJdbc.updateByParameter(sql, sort, id);
        return i > 0 ? new Result() : new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 添加幻灯片
     * @创建人 zm
     * @创建时间 2020/7/10
     */
    @Override
    public Result addCarousel(String url, String link, Integer sort) {
        String sql = "insert into `admin_carousel` (`url`,`link`, `sort`) values(?,?,?)";
        int i = officialWebSiteJdbc.updateByParameter(sql, url, link, sort);
        return i > 0 ? new Result() : new Result(CodeUtil.ADD_FAILURE);
    }

    /**
     * @描述 删除幻灯片
     * @创建人 zm
     * @创建时间 2020/7/10
     */
    @Override
    public Result delCarouselById(Integer id) {
        String sql = "DELETE FROM admin_carousel WHERE id=?";
        int i = officialWebSiteJdbc.updateByParameter(sql, id);
        return i > 0 ? new Result() : new Result(CodeUtil.DELETE_FAILED);
    }


    /**
     * @描述 删除应用
     * @创建人 zm
     * @创建时间 2020/7/10
     */
    @Override
    public Result delApplyById(Integer id) {
        String sql = "DELETE FROM admin_apply WHERE id=?";
        int i = officialWebSiteJdbc.updateByParameter(sql, id);
        return i > 0 ? new Result() : new Result(CodeUtil.DELETE_FAILED);
    }


    /**
     * @描述 添加应用
     * @创建人 zm
     * @创建时间 2020/7/13
     */
    @Override
    public Result addApply(String icon, String name, Integer sort, String url, String introduction, String englishName) {
        String sql = "insert into `admin_apply` (`icon`, `name`, `sort`, `url`, `introduction`,`englishName`) values(?,?,?,?,?,?)";
        logger.info("String icon={}, String name={}, Integer sort={}, String url={}, String introduction={}", icon, name, sort, url, introduction, englishName);
        int i = officialWebSiteJdbc.updateByParameter(sql, icon, name, sort, url, introduction, englishName);
        return i > 0 ? new Result() : new Result(CodeUtil.ADD_FAILURE);
    }

    /**
     * @描述 修改应用
     * @创建人 zm
     * @创建时间 2020/7/13
     */
    @Override
    public Result updateApplyById(Integer id, String icon, String name, Integer sort, String url, String introduction, String englishName) {
        String sql = "UPDATE admin_apply SET icon=?,name=?,sort=?,url=?,introduction=?,englishName=? WHERE id=?";
        int i = officialWebSiteJdbc.updateByParameter(sql, icon, name, sort, url, introduction, englishName, id);
        return i > 0 ? new Result() : new Result(CodeUtil.MODIFICATION_FAILED);
    }


    /**
     * @描述 按条件查询所有应用
     * @创建人 zm
     * @创建时间 2020/7/13
     */
    @Override
    public Result queryApplyAll(Integer indexNo, Integer rows, String name) {
        String sql = "SELECT `id`, `icon`, `name`, `sort`, `url`, `introduction`,`englishName` FROM admin_apply ";
        if (!StringUtils.isEmpty(name)) {
            sql += " WHERE name LIKE '%" + name + "%'";
        }
        List<Map<String, Object>> list = officialWebSiteJdbc.queryListMap(sql);

        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        //返回数据
        return new Result(new PaginationUtil<>(list.size(), rows, tool.getList(), indexNo));
    }


    /**
     * @Author zm
     * @Description 添加标签
     * @Date 2020/9/2 16:27
     */
    @Override
    public Result addLabel(String type, String name, String optionList, String introduction, String englishName) {
        String sql = "insert into `admin_label` (`name`, `type`, `englishName`, `optionList`, `introduction`) values(?,?,?,?,?);";
        int i = officialWebSiteJdbc.updateByParameter(sql, name, type, englishName, optionList, introduction);
        return i > 0 ? new Result() : new Result(CodeUtil.ADD_FAILURE);
    }


    /**
     * @Author zm
     * @Description 查询标签
     * @Date 2020/9/3 9:34
     */
    @Override
    public Result queryLabel() {
        String sql = "SELECT `id`,`name`, `type`, `englishName`, `optionList`, `introduction` FROM admin_label";
        return new Result(officialWebSiteJdbc.queryListMap(sql));
    }

    /**
     * @Author zm
     * @Description 添加关于
     * @Date 2020/9/2 18:49
     */
    @Override
    public Result addAbout(String about) {
        String sql = "DELETE FROM admin_about ";
        officialWebSiteJdbc.updateByParameter(sql);
        sql = "insert into `admin_about` (`introduction`) values(?)";
        int i = officialWebSiteJdbc.updateByParameter(sql, about);
        return i > 0 ? new Result() : new Result(CodeUtil.ADD_FAILURE);
    }


    /**
     * @Author zm
     * @Description 删除标签
     * @Date 2020/9/2 16:27
     */
    @Override
    public Result delLabel(Integer id) {
        String sql = "DELETE FROM admin_label WHERE id=?";
        int i = officialWebSiteJdbc.updateByParameter(sql, id);
        return i > 0 ? new Result() : new Result(CodeUtil.DELETE_FAILED);
    }


    @Override
    public Result queryAbout() {
        String sql = "SELECT `id`,`introduction` FROM admin_about";
        try {
            Map<String, Object> stringObjectMap = officialWebSiteJdbc.queryMap(sql);
            return new Result(stringObjectMap);
        } catch (Exception e) {

        }
        return new Result();
    }


    @Override
    public Result httpRequest(String url) {
        String res = HttpRequestUtil.GetTwo("https://restapi.amap.com/v3/assistant/inputtips?key=d592ce59e9e57e293a39fb60cee5e027&keywords="+url, "", "");
        return new Result(CodeUtil.SUCCESS,res);
    }
}
